<?php

require_once( 'mvc/dynamo-info.php' );

require_once( 'flexical/admin/request-factory.php' );
require_once( 'flexical/admin/object-builder.php' );
require_once( 'flexical/admin/cud-request.php' );
require_once( 'flexical/admin/feed-request.php' );
require_once( 'flexical/admin/feed-cud-request.php' );

class Flexical_Admin_Feed_Request_Factory extends Flexical_Admin_Request_Factory
{
	public function get_request()
	{
		$request_type = $this->get_request_type( $_REQUEST );
		$request = null;

		switch ( $request_type )
		{
			case 'show_feeds':
				$request = new Flexical_Admin_Show_Feeds_Request();
				break;

			case 'create_feed':
				$request = new Flexical_Admin_Feed_CUD_Request( $this->build_feed_object( $_REQUEST ), Flexical_Admin_CUD_Request::$CREATE );
				break;

			case 'edit_feed':
				$request = new Flexical_Admin_Edit_Feed_Request( $this->build_feed_object( $_REQUEST ) );
				break;

			case 'update_feed':
				$request = new Flexical_Admin_Feed_CUD_Request( $this->build_feed_object( $_REQUEST ), Flexical_Admin_CUD_Request::$UPDATE, $this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'confirm_delete_feed':
				$request = new Flexical_Admin_Confirm_Delete_Feed_Request( $this->build_feed_object( $_REQUEST ) );
				break;

			case 'delete_feed':
				$request = new Flexical_Admin_Feed_CUD_Request( $this->build_feed_object( $_REQUEST ), Flexical_Admin_CUD_Request::$DELETE, $this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'test_feed':
				$request = new Flexical_Admin_Test_Feed_Request( $this->build_feed_object( $_REQUEST ) );
				break;

			case 'test_all_feeds':
				$request = new Flexical_Admin_Test_All_Feeds_Request();
				break;

			case 'delete_all_feeds':
				$request = $this->build_delete_all_feeds_request( $_REQUEST );
				break;

			case 'import_feeds':
				$request = $this->build_import_feeds_request( $_REQUEST );
				break;

			default:
				throw new MVC_Exception( "Unsupported request type: " . $request_type );
				break;
		}

		return $request;
	}

	private $feed_object_builder = null;

	private function init_feed_object_builder()
	{
		$map = array(
			'feed_id' => new Flexical_Admin_Object_Property_Spec( 'id', 'int' ),
			'feed_title' => new Flexical_Admin_Object_Property_Spec( 'title', 'string' ),
			'feed_url' => new Flexical_Admin_Object_Property_Spec( 'url', 'string' ),
			'feed_color' => new Flexical_Admin_Object_Property_Spec( 'color', 'string' ),
			'feed_timezone' => new Flexical_Admin_Object_Property_Spec( 'timezone', 'string' ),
		);

		$dynamo_info = new MVC_Dynamo_Info( 'flexical/model/feed.php', 'Flexical_Model_Feed' );

		$this->feed_object_builder = new Flexical_Admin_Object_Builder( $map, $dynamo_info );
	}

	private function build_feed_object( $request_data )
	{
		if ( $feed_object_builder == null )
		{
			$this->init_feed_object_builder();
		}

		return $this->feed_object_builder->build( $request_data );
	}

	private function build_delete_all_feeds_request( $request_data )
	{
		$request = new Flexical_Admin_Delete_All_Feeds_Request();

		$request->confirmed = $this->get_confirmation( $request_data );

		return $request;
	}

	private function build_import_feeds_request( $request_data )
	{
		$request = new Flexical_Admin_Import_Feeds_Request();

		$request->username  = $request_data[ 'username' ];
		$request->password  = $request_data[ 'password' ];
		$request->mode      = $request_data[ 'mode' ];
		$request->confirmed = $this->get_confirmation( $request_data );

		return $request;
	}
}

?>
